﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using PSE.Framework.Cryptography.Util;

namespace PSE.ProcessManager.Server.Process.Configuration
{
    public class ProcessConfigurationElement : ConfigurationElement
    {
        #region Propriedades de Configuracao
        [ConfigurationProperty("isCryptographed")]
        public bool IsCryptographed
        {
            get
            {
                return (bool)this["isCryptographed"];
            }
        }

        [ConfigurationProperty("key")]
        public string Key
        {
            get
            {
                return (string)this["key"];
            }
        }

        [ConfigurationProperty("value")]
        public string Value
        {
            get
            {
                string ret = (string)this["value"];
                //se o dado estiver criptografado
                if (this.IsCryptographed)
                {
                    ret = DESCryptographer.Decrypt(ret, _cryptoKey);
                }

                return ret;
            }
        }
        #endregion

        private byte[] _cryptoKey = null;
        internal void SetCryptoKey(byte[] cryptoKey)
        {
            if (cryptoKey == null)
                throw new ArgumentNullException("cryptoKey");

            _cryptoKey = new byte[cryptoKey.Length];
            cryptoKey.CopyTo(_cryptoKey, 0);
        }
    }
}
